const Base = require('./base.js');
const fs = require('fs');
const moment = require('moment');

module.exports = class extends Base {
  async tempAction() {
    const file = this.file().file;
    if (think.isEmpty(file)) {
      return this.fail('保存失败');
    }
    const that = this;

    const index = file.name.lastIndexOf('.');
    var suffix = '';
    if (index > 0) {
      suffix = file.name.substring(index);
    }
    let staticpath='/static/temp/'+moment().format('YYYYMMDD')+'/';
    let uploadpath = think.ROOT_PATH + '/www'+staticpath;
    think.mkdir(uploadpath);//创建该目录
    const filename = think.uuid(32) + suffix;
    const is = fs.createReadStream(file.path);
    const os = fs.createWriteStream(uploadpath + filename);
    is.pipe(os);
    const fullFileName = this.ctx.origin +staticpath + filename;
    return that.success({
      model_name: 'goods',
      file_name: file.name,
      file_url: fullFileName,
      file_type:file.type
    });
  }

  async categoryPicAction() {
    const categoryFile = this.file('category_pic');
    if (think.isEmpty(categoryFile)) {
      return this.fail('保存失败');
    }
    const that = this;

    const index = categoryFile.path.lastIndexOf('.');
    var suffix = '';
    if (index > 0) {
      suffix = categoryFile.path.substring(index);
    }
    const filename = '/static/upload/category/' + think.uuid(32) + suffix;
    const is = fs.createReadStream(categoryFile.path);
    const os = fs.createWriteStream(think.ROOT_PATH + '/www' + filename);
    is.pipe(os);
    const fullFileName = this.ctx.origin + filename;
    return that.success({
      name: 'category_pic',
      fileUrl: filename,
      fullFileUrl: fullFileName
    });
  }

  async uploadPicAction() {
    const categoryFile = this.file('pic');
    if (think.isEmpty(categoryFile)) {
      return this.fail('保存失败');
    }
    const that = this;

    const index = categoryFile.path.lastIndexOf('.');
    var suffix = '';
    if (index > 0) {
      suffix = categoryFile.path.substring(index);
    }
    const filename = '/static/upload/pic/' + think.uuid(32) + suffix;
    const fullFileName = this.ctx.origin + filename;
    const is = fs.createReadStream(categoryFile.path);
    const os = fs.createWriteStream(think.ROOT_PATH + '/www' + filename);
    is.pipe(os);
    return that.success({
      name: 'pic',
      fileUrl: filename,
      fullFileUrl: fullFileName
    });
  }

  async uploadEditorPicAction() {
    const categoryFile = this.file('editorpic');
    if (think.isEmpty(categoryFile)) {
      return this.fail('保存失败');
    }
    const that = this;

    const index = categoryFile.path.lastIndexOf('.');
    var suffix = '';
    if (index > 0) {
      suffix = categoryFile.path.substring(index);
    }
    const filename = '/static/upload/editorpic/' + think.uuid(32) + suffix;
    const fullFileName = this.ctx.origin + filename;
    const is = fs.createReadStream(categoryFile.path);
    const os = fs.createWriteStream(think.ROOT_PATH + '/www' + filename);
    is.pipe(os);
    return that.success({
      name: 'pic',
      fileUrl: filename,
      fullFileUrl: fullFileName
    });
  }

  async reportTopPicAction() {
    const categoryFile = this.file('top_pic');
    if (think.isEmpty(categoryFile)) {
      return this.fail('保存失败');
    }
    const that = this;

    const index = categoryFile.path.lastIndexOf('.');
    var suffix = '';
    if (index > 0) {
      suffix = categoryFile.path.substring(index);
    }
    const filename = '/static/upload/report/' + think.uuid(32) + suffix;
    const is = fs.createReadStream(categoryFile.path);
    const os = fs.createWriteStream(think.ROOT_PATH + '/www' + filename);
    const fullFileName = this.ctx.origin + filename;
    is.pipe(os);
    return that.success({
      name: 'top_pic',
      fileUrl: filename,
      fullFileUrl: fullFileName
    });
  }

  async reportFootPicAction() {
    const categoryFile = this.file('foot_pic');
    if (think.isEmpty(categoryFile)) {
      return this.fail('保存失败');
    }
    const that = this;

    const index = categoryFile.path.lastIndexOf('.');
    var suffix = '';
    if (index > 0) {
      suffix = categoryFile.path.substring(index);
    }
    const filename = '/static/upload/report/' + think.uuid(32) + suffix;
    const fullFileName = this.ctx.origin + filename;
    const is = fs.createReadStream(categoryFile.path);
    const os = fs.createWriteStream(think.ROOT_PATH + '/www' + filename);
    is.pipe(os);
    return that.success({
      name: 'foot_pic',
      fileUrl: filename,
      fullFileUrl: fullFileName
    });
  }

  async reportQRPicAction() {
    const categoryFile = this.file('qr_pic');
    if (think.isEmpty(categoryFile)) {
      return this.fail('保存失败');
    }
    const that = this;

    const index = categoryFile.path.lastIndexOf('.');
    var suffix = '';
    if (index > 0) {
      suffix = categoryFile.path.substring(index);
    }
    const filename = '/static/upload/report/' + think.uuid(32) + suffix;
    const is = fs.createReadStream(categoryFile.path);
    const os = fs.createWriteStream(think.ROOT_PATH + '/www' + filename);
    const fullFileName = this.ctx.origin + filename;
    is.pipe(os);
    return that.success({
      name: 'qr_pic',
      fileUrl: filename,
      fullFileUrl: fullFileName
    });
  }
  async operateFileAction() {//上传运营分类文件图片
    const file = this.file().file;
    if (think.isEmpty(file)) {
      return this.fail('保存失败');
    }
    const that = this;

    const index = file.path.lastIndexOf('.');
    var suffix = '';
    if (index > 0) {
      suffix = file.path.substring(index);
    }
    let staticpath='/static/upload/operate/';
    let uploadpath = think.ROOT_PATH + '/www'+staticpath;
    think.mkdir(uploadpath);//创建该目录
    const filename = think.uuid(32) + suffix;
    const is = fs.createReadStream(file.path);
    const os = fs.createWriteStream(uploadpath + filename);
    is.pipe(os);
    const fullFileName = this.ctx.origin +staticpath + filename;
    return that.success({
      model_name: 'operate',
      file_name: file.name,
      file_url: fullFileName,
      file_type:file.type
    });
  }
  async goodsFileAction() {//上传商品文件图片
    const file = this.file().file;
    if (think.isEmpty(file)) {
      return this.fail('保存失败');
    }
    const that = this;

    const index = file.path.lastIndexOf('.');
    var suffix = '';
    if (index > 0) {
      suffix = file.path.substring(index);
    }
    let staticpath='/static/upload/goods/';
    let uploadpath = think.ROOT_PATH + '/www'+staticpath;
    think.mkdir(uploadpath);//创建该目录
    const filename = think.uuid(32) + suffix;
    const is = fs.createReadStream(file.path);
    const os = fs.createWriteStream(uploadpath + filename);
    is.pipe(os);
    const fullFileName = this.ctx.origin +staticpath + filename;
    return that.success({
      model_name: 'goods',
      file_name: file.name,
      file_url: fullFileName,
      file_type:file.type
    });
  }
  async avatarAction() {//上传头像
    const file = this.file().file;
    if (think.isEmpty(file)) {
      return this.fail('保存失败');
    }
    const that = this;

    const index = file.path.lastIndexOf('.');
    var suffix = '';
    if (index > 0) {
      suffix = file.path.substring(index);
    }
    let staticpath='/static/upload/avatar/';
    let uploadpath = think.ROOT_PATH + '/www'+staticpath;
    think.mkdir(uploadpath);//创建该目录
    const filename = think.uuid(32) + suffix;
    const is = fs.createReadStream(file.path);
    const os = fs.createWriteStream(uploadpath + filename);
    is.pipe(os);
    const fullFileName = this.ctx.origin +staticpath + filename;
    return that.success({
      model_name: 'avatar',
      file_name: file.name,
      file_url: fullFileName,
      file_type:file.type
    });
  }
};
